import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { allProjects } from '#/api/clinical/project';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';
import { getPopupContainer } from '@vben/utils';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'name',
    label: '问卷名称',
  },
];

// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '问卷名称',
    field: 'name',
  },
  {
    title: '所属项目',
    field: 'projectName',
  },
  {
    title: '问卷类型',
    field: 'type',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.SYS_USER_SEX 便于维护
        return renderDict(row.type, 'survey_type');
      },
    },
  },
  {
    title: '创建时间',
    field: 'createTime',
  },
  {
    title: '创建人',
    field: 'createByName',
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: '问卷id',
    fieldName: 'id',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '问卷名称',
    fieldName: 'name',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '问卷类型',
    fieldName: 'type',
    component: 'RadioGroup',
    defaultValue: 1,
    componentProps: {
      options: getDictOptions('survey_type', true),
    },
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      getPopupContainer,
      api: () => allProjects(),
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
    fieldName: 'researchProjectId',
    label: '所属项目',
    rules: 'required',
  },
];
